package com.pet.server.ext.service;

import java.util.List;

import com.pet.server.ext.ApiError;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Page;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.dbutil.JdbcTemplate;
import com.pet.server.ext.dbutil.Sql;


public class ProductService {
	private JdbcTemplate jdbcTemplate = new JdbcTemplate();
	public void searchProductInfo(Page page,String cate,String[] pet_cate,String[] product_cate,String[] product_age,String[] product_brand,Double min_price,Double max_price) throws ApiError{
		Sql sql = new Sql("SELECT p.* FROM product_info p WHERE p.state=?");
		    sql.addParam(BaseAttribute.LOGIC_TRUE);
		    sql.appendLike("^", "AND", "p.product_cate", cate);
		    sql.appendIn("AND", "p.product_cate", product_cate);
			sql.appendIn("AND", "p.pet_cate", pet_cate);
			//sql.appendIn("AND", "p.product_cate", product_cate);
			sql.appendIn("AND", "p.product_age", product_age);
			sql.appendIn("AND", "p.product_brand", product_brand);
			sql.append("AND", "p.product_price", ">=", min_price);
			sql.append("AND", "p.product_price", "<=", max_price);
	        sql.append(" ORDER BY p.id DESC");
		jdbcTemplate.search(page, sql, ProductInfo.class);
	}
	public List<ProductInfo> queryProductInfo(Integer size) throws ApiError{
		Sql sql = new Sql("SELECT p.* FROM product_info p WHERE 1=1");
		    sql.append(" ORDER BY p.id DESC");
	    return jdbcTemplate.queryForList(sql, ProductInfo.class,size);
	}
	
	public List<ProductInfo> queryProductInfoForDating(String code,Integer size) throws ApiError{
		Sql sql = new Sql("SELECT p.* FROM product_data p WHERE 1=1");
		    sql.appendLike(null, "and", "p.product_cate", code);
		    sql.append(" ORDER BY p.id DESC");
	    return jdbcTemplate.queryForList(sql, ProductInfo.class,size);
	}
	
	
	
	public ProductInfo findProductInfoByProductId(Integer productId) throws ApiError{
		Sql sql = new Sql("select * from product_info p WHERE p.id=?");
		    sql.addParam(productId);
		return jdbcTemplate.findObject(sql, ProductInfo.class);
	}
	
}
